package com.hy.formula.function;

import java.math.BigDecimal;

import com.hy.formula.common.BigDecimalUtils;
import com.hy.formula.common.CollectionUtils;
import com.hy.formula.runtime.FRuntimeContext;

public class MultiFunc extends AbsNumCalculateFunc implements IFunction {

	@Override
	public String getName() {
		return "multi";
	}

	@Override
	public Object execute(FRuntimeContext context, Object... params) {
		if(!CollectionUtils.isEmpty(params)){
			//判断传入的数值中哪个哪个类型比较高级
			boolean floatFlag = false;
			boolean doubleFlag = false;
			boolean bigDecimalFlag = false;
			for(Object obj : params){
				if(obj instanceof BigDecimal) {
					bigDecimalFlag = true;
				}else if(obj instanceof Double){
					doubleFlag = true;
				}else if(obj instanceof Float){
					floatFlag = true;
				}
			}
			
			if(bigDecimalFlag) {
				BigDecimal result = BigDecimal.ONE;
				for(Object param : params) {
					result = BigDecimalUtils.multi(result,new BigDecimal(param.toString()));
				}
				return result;
			}else if(doubleFlag){
				//转成双精度
				double result = 1.0d;
				for(Object param : params){
					String num = param.toString();
					result *= Double.parseDouble(num);
				}
				return result;
			}else if(floatFlag){
				//转成浮点数计算
				float result = 1.0f;
				for(Object param : params){
					String num = param.toString();
					result *= Float.parseFloat(num);
				}
				return result;
			}else{
				//全部用整型计算
				Integer result = 1;
				for(Object param : params){
					result *= Integer.parseInt(param.toString());
				}
				return result;
			}
		}else{
			throw new IllegalArgumentException("请输入参数");
		}
	}

	@Override
	public String description() {
		return "乘法,支持多参数";
	}

}
